﻿using BarcodePrinting;
using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.FormatOutput;
using OrderSystem.SystemFramework;
using OrderSystem.Templet;
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormFlowcardInfo : FormTemplet
    {
        private DataTable _dtOrderItems = null;

        private long _nFlowcardID = -1L;

        private string _sFlowcardCode = null;

        private int _nCustomerID = -1;

        private string _sCompanyName = null;

        private string _sCompanyLinkman = null;

        private int _nProjectID = -1;

        private string _sProjectName = null;

        private string _sProjectLinkman = null;

        private string _sProjectLinkmanPhone = null;

        private string _sProjectAddress = null;

        private string _sOrderDate = null;

        private string _sSpeakForDate = null;

        private string _sDeliveryDate = null;

        private string _sOrderRemark = null;

        private string _sFlowcardRemark = null;

        private string _sLabelName = null;

        private int _nOperatorID = -1;

        private string _sOperatorName = null;

        private string _sSalesPerson = null;

        private FormAddOrderItem frmAddOrderItem;

        private FormUpdateOrderItem frmUpdateOrderItem;

        private string _sOriginalValue = "";


        public FormFlowcardInfo()
        {
            this.InitializeComponent();
        }

        public FormFlowcardInfo(string sFlowcardCode) : this()
        {
            this._sFlowcardCode = sFlowcardCode;
            this.RefreshFlowcard();
            this.RefreshItems();
        }

        private void RefreshFlowcard()
        {
            DataTable dataTable = null;
            Flowcards flowcards = new Flowcards();
            try
            {
                dataTable = flowcards.LoadFlowcardByFlowcardCode(this._sFlowcardCode);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            flowcards.Dispose();
            if (dataTable != null)
            {
                if (dataTable.Rows.Count > 0)
                {
                    this._nFlowcardID = (long)Convert.ToInt32(dataTable.Rows[0]["流程卡序号"]);
                    this._nCustomerID = Convert.ToInt32(dataTable.Rows[0]["客户序号"]);
                    this._sCompanyName = dataTable.Rows[0]["公司名称"].ToString();
                    this._sCompanyLinkman = dataTable.Rows[0]["公司联系人"].ToString();
                    this._nProjectID = Convert.ToInt32(dataTable.Rows[0]["计划序号"]);
                    this._sProjectName = dataTable.Rows[0]["计划名称"].ToString();
                    this._sProjectLinkman = dataTable.Rows[0]["计划联系人"].ToString();
                    this._sProjectLinkmanPhone = dataTable.Rows[0]["计划联系人电话"].ToString();
                    this._sProjectAddress = dataTable.Rows[0]["计划地址"].ToString();
                    this._sOrderDate = dataTable.Rows[0]["下单日期"].ToString();
                    this._sSpeakForDate = dataTable.Rows[0]["订单日期"].ToString();
                    this._sDeliveryDate = DateTime.Parse(dataTable.Rows[0]["交货日期"].ToString()).ToShortDateString();
                    this._sOrderRemark = dataTable.Rows[0]["订单备注"].ToString();
                    this._sFlowcardRemark = dataTable.Rows[0]["流程卡备注"].ToString();
                    this._sLabelName = dataTable.Rows[0]["标签名称"].ToString();
                    this._nOperatorID = Convert.ToInt32(dataTable.Rows[0]["操作者ID"]);
                    this._sOperatorName = dataTable.Rows[0]["操作者"].ToString();
                    this._sSalesPerson = dataTable.Rows[0]["业务员"].ToString();
                    this.txtOrderCode.Text = this._sFlowcardCode;
                    this.txtCompanyName.Text = string.Concat(new string[]
                    {
                        this._sCompanyName,
                        "(",
                        this._sProjectName,
                        "<",
                        this._sProjectLinkman,
                        ">)"
                    });
                    this.txtProjectAddress.Text = this._sProjectAddress + "(" + this._sProjectLinkmanPhone + ")";
                    this.txtOrderDate.Text = this._sOrderDate;
                    this.txtSpeakForDate.Text = this._sSpeakForDate + "  交货日期:" + this._sDeliveryDate;
                    this.txtRemark.Text = ((this._sOrderRemark != "" && this._sFlowcardRemark != "") ? (this._sOrderRemark + "/" + this._sFlowcardRemark) : (this._sOrderRemark + this._sFlowcardRemark));
                    this.txtLabelName.Text = this._sLabelName;
                    this.txtOperatorName.Text = this._sOperatorName;
                    this.txtSalesPerson.Text = this._sSalesPerson;
                }
                else
                {
                    MessageBox.Show("没有查询到此订单", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                dataTable.Dispose();
            }
        }

        private void RefreshItems()
        {
            DataTable dataTable = null;
            FlowcardItems flowcardItems = new FlowcardItems();
            try
            {
                Sequence eSequence;
                if (this.menuDataSequenceByInput.Checked)
                {
                    eSequence = Sequence.ByPKID;
                }
                else
                {
                    eSequence = Sequence.BySize;
                }
                dataTable = flowcardItems.GetItems(this._nFlowcardID, eSequence);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！\r\n\r\n订单项信息可能没有读出来", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message + "\r\n\r\n订单项信息可能没有读出来", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            flowcardItems.Dispose();
            if (dataTable != null)
            {
                if (this._dtOrderItems != null)
                {
                    this._dtOrderItems.Dispose();
                    this._dtOrderItems = null;
                }
                this._dtOrderItems = dataTable;
                this.dgProducts.DataSource = this._dtOrderItems;
                this.dgProducts.AutoResizeColumns();
                bool flag = false;
                for (int i = 0; i < this._dtOrderItems.Rows.Count; i++)
                {
                    if (this._dtOrderItems.Rows[i]["宽(短边)"].ToString().Length > 0 || this._dtOrderItems.Rows[i]["高(短边)"].ToString().Length > 0)
                    {
                        flag = true;
                        this.dgProducts.Columns["宽(短边)"].Visible = true;
                        this.dgProducts.Columns["高(短边)"].Visible = true;
                        break;
                    }
                }
                if (!flag)
                {
                    this.dgProducts.Columns["宽(短边)"].Visible = false;
                    this.dgProducts.Columns["高(短边)"].Visible = false;
                }
            }
            else
            {
                MessageBox.Show("加载此订单的数据失败！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        public void ReloadOrderItemsForInsert(string sProductID, string sProductType, string sUnit, string sProductCode, decimal mWidth, string sSubtenseOfWidth, decimal mHeight, string sSubtenseOfHeight, int nQuantity, decimal mPrice, string sRemark, string sTechnics)
        {
            if (this.menuShowData.Checked)
            {
                if (this._dtOrderItems == null)
                {
                    MessageBox.Show("数据容器是空，可能还有数据保存在数据库，但没有加载到容器中，你可以从新打开此订单试一试，但是现有数据将加载在此容器中", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this._dtOrderItems = new DataTable("OrderItem");
                    this._dtOrderItems.Columns.Add(new DataColumn("产品ID", typeof(string)));
                    this._dtOrderItems.Columns.Add(new DataColumn("类型", typeof(string)));
                    this._dtOrderItems.Columns.Add(new DataColumn("单位", typeof(string)));
                    this._dtOrderItems.Columns.Add(new DataColumn("编号", typeof(string)));
                    this._dtOrderItems.Columns.Add(new DataColumn("宽", typeof(decimal)));
                    this._dtOrderItems.Columns.Add(new DataColumn("宽(短边)", typeof(string)));
                    this._dtOrderItems.Columns.Add(new DataColumn("高", typeof(decimal)));
                    this._dtOrderItems.Columns.Add(new DataColumn("高(短边)", typeof(string)));
                    this._dtOrderItems.Columns.Add(new DataColumn("数量", typeof(int)));
                    this._dtOrderItems.Columns.Add(new DataColumn("面积", typeof(decimal)));
                    this._dtOrderItems.Columns.Add(new DataColumn("价格", typeof(decimal)));
                    this._dtOrderItems.Columns.Add(new DataColumn("备注", typeof(string)));
                    this._dtOrderItems.Columns.Add(new DataColumn("工艺", typeof(string)));
                    this.dgProducts.DataSource = this._dtOrderItems;
                }
                DataRow dataRow = this._dtOrderItems.NewRow();
                dataRow[0] = sProductID;
                dataRow[1] = sProductType;
                dataRow[2] = sUnit;
                dataRow[3] = sProductCode;
                dataRow[4] = mWidth;
                dataRow[5] = sSubtenseOfWidth;
                dataRow[6] = mHeight;
                dataRow[7] = sSubtenseOfHeight;
                dataRow[8] = nQuantity;
                dataRow[9] = Convert.ToDouble(Convert.ToDecimal((mWidth * mHeight / 1000000m).ToString("F4")) * nQuantity);
                dataRow[10] = Convert.ToDouble(mPrice);
                dataRow[11] = sRemark;
                dataRow[12] = sTechnics;
                this._dtOrderItems.Rows.Add(dataRow);
                this._dtOrderItems.AcceptChanges();
                for (int i = 0; i < this._dtOrderItems.Rows.Count; i++)
                {
                    if (this.dgProducts.Rows[i].Cells[0].Value.ToString() == sProductID)
                    {
                        this.dgProducts.CurrentCell = this.dgProducts.Rows[i].Cells[0];
                        this.dgProducts.Rows[i].Selected = true;
                        break;
                    }
                }
                this.dgProducts.AutoResizeColumns();
            }
        }

        public void ReloadOrderItemsForUpdate(string sProductID, string sProductType, string sUnit, string sProductCode, decimal mWidth, string sSubtenseOfWidth, decimal mHeight, string sSubtenseOfHeight, int nQuantity, decimal mPrice, string sRemark, string sTechnics)
        {
            if (this._dtOrderItems == null)
            {
                MessageBox.Show("数据容器是空，不能装载数据", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                for (int i = 0; i < this._dtOrderItems.Rows.Count; i++)
                {
                    if (sProductID == this._dtOrderItems.Rows[i][0].ToString())
                    {
                        DataRow dataRow = this._dtOrderItems.Rows[i];
                        dataRow[0] = sProductID;
                        dataRow[1] = sProductType;
                        dataRow[2] = sUnit;
                        dataRow[3] = sProductCode;
                        dataRow[4] = mWidth;
                        dataRow[5] = sSubtenseOfWidth;
                        dataRow[6] = mHeight;
                        dataRow[7] = sSubtenseOfHeight;
                        dataRow[8] = nQuantity;
                        dataRow[9] = Convert.ToDouble(Convert.ToDecimal((mWidth * mHeight / 1000000m).ToString("F4")) * nQuantity);
                        dataRow[10] = Convert.ToDouble(mPrice);
                        dataRow[11] = sRemark;
                        dataRow[12] = sTechnics;
                        break;
                    }
                }
                this._dtOrderItems.AcceptChanges();
                for (int i = 0; i < this._dtOrderItems.Rows.Count; i++)
                {
                    if (this.dgProducts.Rows[i].Cells[0].Value.ToString() == sProductID)
                    {
                        this.dgProducts.CurrentCell = this.dgProducts.Rows[i].Cells[0];
                        this.dgProducts.Rows[i].Selected = true;
                        break;
                    }
                }
                this.dgProducts.AutoResizeColumns();
            }
        }

        public void ReloadOrderItemsForUpdate(string sProductID, string sTechnics)
        {
            if (this._dtOrderItems == null)
            {
                MessageBox.Show("数据容器是空，不能装载数据", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                for (int i = 0; i < this._dtOrderItems.Rows.Count; i++)
                {
                    if (sProductID == this._dtOrderItems.Rows[i][0].ToString())
                    {
                        DataRow dataRow = this._dtOrderItems.Rows[i];
                        dataRow[0] = sProductID;
                        dataRow[12] = sTechnics;
                        break;
                    }
                }
                this._dtOrderItems.AcceptChanges();
                this.dgProducts.AutoResizeColumns();
            }
        }

        private void FormOrderInfo_Load(object sender, EventArgs e)
        {
            this.dgProducts.AutoResizeColumns();
        }

        private void btnAddOrderItem_Click(object sender, EventArgs e)
        {
            this.menuAddOrderItem_Click(null, null);
        }

        private void menuAddOrderItem_Click(object sender, EventArgs e)
        {
        }

        private void btnUpdateOrderItem_Click(object sender, EventArgs e)
        {
            this.menuUpdateOrderItem_Click(null, null);
        }

        private void menuUpdateOrderItem_Click(object sender, EventArgs e)
        {
        }

        private void btnDeleteOrderItem_Click(object sender, EventArgs e)
        {
            this.menuDeleteOrderItem_Click(null, null);
        }

        private void dgOrders_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                this.menuDeleteOrderItem_Click(null, null);
            }
        }

        private void menuDeleteOrderItem_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.OrderData_Update])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                try
                {
                    string text = this.dgProducts.Rows[0].Cells[0].Value.ToString();
                }
                catch
                {
                    MessageBox.Show("表格中没有数据", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                if (MessageBox.Show("是否真的要删除这些选中的数据吗 ？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) != DialogResult.Cancel)
                {
                    OrderItems orderItems = new OrderItems();
                    try
                    {
                        ArrayList arrayList = new ArrayList();
                        for (int i = 0; i < this.dgProducts.SelectedRows.Count; i++)
                        {
                            arrayList.Add(this.dgProducts.SelectedRows[i].Cells[0].Value.ToString());
                        }
                        for (int i = 0; i < arrayList.Count; i++)
                        {
                            orderItems.DeleteOrderItem(arrayList[i].ToString(), LoginInfo.OperatorID);
                            for (int j = 0; j < this._dtOrderItems.Rows.Count; j++)
                            {
                                if (this._dtOrderItems.Rows[j][0].ToString() == arrayList[i].ToString())
                                {
                                    DataRow dataRow = this._dtOrderItems.Rows[j];
                                    dataRow.Delete();
                                    dataRow.AcceptChanges();
                                    break;
                                }
                            }
                        }
                    }
                    catch (SqlException ex)
                    {
                        int number = ex.Number;
                        if (number != 50001)
                        {
                            if (number != 50103)
                            {
                                if (number != 50200)
                                {
                                    MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                }
                                else
                                {
                                    MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                }
                            }
                            else
                            {
                                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                        }
                        else
                        {
                            MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }
                    catch (Exception ex2)
                    {
                        MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    orderItems.Dispose();
                }
            }
        }

        private void menuRefreshOrder_Click(object sender, EventArgs e)
        {
            this.RefreshFlowcard();
        }

        private void menuRefreshItems_Click(object sender, EventArgs e)
        {
            this.RefreshItems();
        }

        private DataSet GetDataForFlowcard()
        {
            FlowcardItems flowcardItems = new FlowcardItems();
            DataSet dataSet = null;
            try
            {
                Sequence eSequence;
                if (this.menuDataSequenceByInput.Checked)
                {
                    eSequence = Sequence.ByPKID;
                }
                else
                {
                    eSequence = Sequence.BySize;
                }
                dataSet = flowcardItems.GetItemsForFlowcard(this._nFlowcardID, eSequence);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            flowcardItems.Dispose();
            DataSet result;
            if (dataSet != null)
            {
                if (dataSet.Tables.Count == 2)
                {
                    if (dataSet.Tables[0].Rows.Count > 0)
                    {
                        result = dataSet;
                    }
                    else
                    {
                        MessageBox.Show("此订单中还没有数据", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        dataSet.Dispose();
                        result = null;
                    }
                }
                else
                {
                    result = null;
                }
            }
            else
            {
                result = null;
            }
            return result;
        }

        private DataTable GetDataForLabel()
        {
            FlowcardItems flowcardItems = new FlowcardItems();
            DataTable dataTable = null;
            try
            {
                Sequence eSequence;
                if (this.menuDataSequenceByInput.Checked)
                {
                    eSequence = Sequence.ByPKID;
                }
                else
                {
                    eSequence = Sequence.BySize;
                }
                dataTable = flowcardItems.GetDataForLabel(this._nFlowcardID, eSequence);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            flowcardItems.Dispose();
            DataTable result;
            if (dataTable != null)
            {
                if (dataTable.Rows.Count < 1)
                {
                    MessageBox.Show("此订单中还没有数据", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    dataTable.Dispose();
                    result = null;
                }
                else
                {
                    result = dataTable;
                }
            }
            else
            {
                result = null;
            }
            return result;
        }

        private void btnCreateFlowcard_Click(object sender, EventArgs e)
        {
            this.menuCreateFlowcard_Click(null, null);
        }

        private void menuCreateFlowcard_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.Order_Print])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (this._nFlowcardID == -1L)
            {
                MessageBox.Show("加载数据时出现了错误", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                Flowcards flowcards = new Flowcards();
                int num = -2;
                try
                {
                    num = flowcards.GetPrintedQuantity(this._nFlowcardID);
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                flowcards.Dispose();
                if (num > -1)
                {
                    if (num > 0)
                    {
                        if (MessageBox.Show("此单已经生成了 " + num.ToString() + " 次，是否继续此次生成？", "提示！", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                        {
                            return;
                        }
                    }
                }
                else if (num == -1)
                {
                    MessageBox.Show("没有找到此单！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                DataSet dataForFlowcard = this.GetDataForFlowcard();
                if (dataForFlowcard != null)
                {
                    this.saveFileDialog1.Filter = "Excel文件|*.xls";
                    this.saveFileDialog1.Title = "请选择你要输入的路径和文件名";
                    this.saveFileDialog1.FileName = this.txtOrderCode.Text.Replace("/", "／") + ".xls";
                    string text = null;
                    if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        text = this.saveFileDialog1.FileName;
                        this.saveFileDialog1.Dispose();
                        try
                        {
                            File.Copy(Application.StartupPath + "\\model\\" + AppSettings.GetExcelSettings("flowcard"), text, true);
                        }
                        catch (IOException ex3)
                        {
                            MessageBox.Show(ex3.Message + "\r\n或者是: 和你要创建的相同文件名的目标文件正在使用，不能覆盖，请把它关闭或删除后在操作", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            dataForFlowcard.Dispose();
                            return;
                        }
                        catch (Exception ex2)
                        {
                            MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            dataForFlowcard.Dispose();
                            return;
                        }
                        flowcards = new Flowcards();
                        bool flag = false;
                        try
                        {
                            flag = flowcards.PrintOnce(this._nFlowcardID);
                        }
                        catch (SqlException ex)
                        {
                            MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                        catch (Exception ex2)
                        {
                            MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                        flowcards.Dispose();
                        if (flag)
                        {
                            try
                            {
                                ExcelOperationFlowcard excelOperationFlowcard = new ExcelOperationFlowcard(Application.StartupPath);
                                excelOperationFlowcard.MakeOrderDataToExcel(text, dataForFlowcard, this._sFlowcardCode, this._sCompanyName, this._sProjectName, DateTime.Parse(this._sSpeakForDate).ToShortDateString(), DateTime.Parse(this._sDeliveryDate).ToShortDateString(), this._sProjectLinkman, this._sProjectLinkmanPhone, this._sProjectAddress, this.txtRemark.Text, LoginInfo.OperatorName, this._sSalesPerson, "");
                            }
                            catch (Exception ex2)
                            {
                                MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                            dataForFlowcard.Dispose();
                        }
                    }
                    else
                    {
                        this.saveFileDialog1.Dispose();
                        dataForFlowcard.Dispose();
                    }
                }
            }
        }

        private void btnPrintBarCodeLabel_Click(object sender, EventArgs e)
        {
            this.menuPrintBarCodeLabel_Click(null, null);
        }

        private void menuPrintBarCodeLabel_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.Order_PrintLabel])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                Flowcards flowcards = new Flowcards();
                int num = -2;
                try
                {
                    num = flowcards.GetPrintedQuantityForLabel(this._nFlowcardID);
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                flowcards.Dispose();
                if (num > -1)
                {
                    if (num > 0)
                    {
                        if (MessageBox.Show("此单标签已经打印了 " + num.ToString() + " 次，是否继续此次打印？", "提示！", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                        {
                            return;
                        }
                    }
                }
                else if (num == -1)
                {
                    MessageBox.Show("没有找到此单！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                DataTable dataForLabel = this.GetDataForLabel();
                if (dataForLabel != null)
                {
                    LabelModel[] array = new LabelModel[dataForLabel.Rows.Count];
                    for (int i = 0; i < dataForLabel.Rows.Count; i++)
                    {
                        array[i] = new LabelModel(dataForLabel.Rows[i]["标签名称"].ToString(), dataForLabel.Rows[i]["公司名称"].ToString(), dataForLabel.Rows[i]["计划名称"].ToString(), dataForLabel.Rows[i]["产品ID"].ToString(), dataForLabel.Rows[i]["产品类型"].ToString(), dataForLabel.Rows[i]["类型单位"].ToString(), dataForLabel.Rows[i]["产品编号"].ToString(), Convert.ToDouble(dataForLabel.Rows[i]["宽"]), dataForLabel.Rows[i]["宽(短边)"].ToString(), Convert.ToDouble(dataForLabel.Rows[i]["高"]), dataForLabel.Rows[i]["高(短边)"].ToString(), dataForLabel.Rows[i]["加工要求"].ToString(), Convert.ToInt32(dataForLabel.Rows[i]["产品数量"]), Convert.ToDecimal(dataForLabel.Rows[i]["产品价格"]), dataForLabel.Rows[i]["产品备注"].ToString(), dataForLabel.Rows[i]["产品工艺"].ToString(), Convert.ToInt32(dataForLabel.Rows[i]["标签倍数"]), Convert.ToInt32(dataForLabel.Rows[i]["产品数量"]), Convert.ToInt32(dataForLabel.Rows[i]["标签倍数"]), "");
                    }
                    if (MessageBox.Show("是否按照数据库打印？", "提示！", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                    {
                        string productType = array[0].ProductType;
                        string text = InputBox.ShowBox("请输入你要打印标签所要更改的类型...", productType);
                        if (text == null)
                        {
                            return;
                        }
                        if (text.Trim().Length <= 0)
                        {
                            MessageBox.Show("你没有输入数据！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            return;
                        }
                        for (int i = 0; i < array.Length; i++)
                        {
                            if (array[i].ProductType == productType)
                            {
                                array[i].ProductType = text;
                            }
                            else
                            {
                                productType = array[i].ProductType;
                                text = InputBox.ShowBox("请输入你要打印标签所要更改的类型...", productType);
                                if (text == null)
                                {
                                    return;
                                }
                                if (text.Trim().Length <= 0)
                                {
                                    MessageBox.Show("你没有输入数据！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                    return;
                                }
                                array[i].ProductType = text;
                            }
                        }
                    }
                    flowcards = new Flowcards();
                    bool flag = false;
                    try
                    {
                        flag = flowcards.PrintOnceForLabel(this._nFlowcardID);
                    }
                    catch (SqlException ex)
                    {
                        MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    catch (Exception ex2)
                    {
                        MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    flowcards.Dispose();
                    if (flag)
                    {
                        try
                        {
                            BarcodePrinter barcodePrinter = new Printer_BiaoZhun_Tsc();
                            barcodePrinter.Print(array);
                        }
                        catch (Exception ex2)
                        {
                            MessageBox.Show(ex2.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }
                }
            }
        }

        private void menuPrintChoiceLabel_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.Order_PrintLabel])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                DataTable dataForLabel = this.GetDataForLabel();
                if (dataForLabel != null)
                {
                    bool flag = true;
                    if (MessageBox.Show("按照数量打印，请点击是\r\n每个标签打印一个，请点击否", "提示！", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                    {
                        flag = false;
                    }
                    if (this.dgProducts.SelectedRows.Count > 0)
                    {
                        int num = 0;
                        LabelModel[] array = new LabelModel[this.dgProducts.SelectedRows.Count];
                        for (int i = 0; i < dataForLabel.Rows.Count; i++)
                        {
                            for (int j = 0; j < this.dgProducts.SelectedRows.Count; j++)
                            {
                                if (dataForLabel.Rows[i]["产品ID"].ToString() == this.dgProducts.SelectedRows[j].Cells["产品ID"].Value.ToString())
                                {
                                    array[num] = new LabelModel(dataForLabel.Rows[i]["标签名称"].ToString(), dataForLabel.Rows[i]["公司名称"].ToString(), dataForLabel.Rows[i]["计划名称"].ToString(), dataForLabel.Rows[i]["产品ID"].ToString(), dataForLabel.Rows[i]["产品类型"].ToString(), dataForLabel.Rows[i]["类型单位"].ToString(), dataForLabel.Rows[i]["产品编号"].ToString(), Convert.ToDouble(dataForLabel.Rows[i]["宽"]), dataForLabel.Rows[i]["宽(短边)"].ToString(), Convert.ToDouble(dataForLabel.Rows[i]["高"]), dataForLabel.Rows[i]["高(短边)"].ToString(), dataForLabel.Rows[i]["加工要求"].ToString(), Convert.ToInt32(dataForLabel.Rows[i]["产品数量"]), Convert.ToDecimal(dataForLabel.Rows[i]["产品价格"]), dataForLabel.Rows[i]["产品备注"].ToString(), dataForLabel.Rows[i]["产品工艺"].ToString(), Convert.ToInt32(dataForLabel.Rows[i]["标签倍数"]), (!flag) ? 1 : Convert.ToInt32(dataForLabel.Rows[i]["产品数量"]), (!flag) ? 1 : Convert.ToInt32(dataForLabel.Rows[i]["标签倍数"]), "");
                                    num++;
                                    break;
                                }
                            }
                        }
                        if (MessageBox.Show("是否按照数据库打印？", "提示！", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                        {
                            string productType = array[0].ProductType;
                            string text = InputBox.ShowBox("请输入你要打印标签所要更改的类型...", productType);
                            if (text == null)
                            {
                                return;
                            }
                            if (text.Trim().Length <= 0)
                            {
                                MessageBox.Show("你没有输入数据！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                return;
                            }
                            for (int i = 0; i < array.Length; i++)
                            {
                                if (array[i].ProductType == productType)
                                {
                                    array[i].ProductType = text;
                                }
                                else
                                {
                                    productType = array[i].ProductType;
                                    text = InputBox.ShowBox("请输入你要打印标签所要更改的类型...", productType);
                                    if (text == null)
                                    {
                                        return;
                                    }
                                    if (text.Trim().Length <= 0)
                                    {
                                        MessageBox.Show("你没有输入数据！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                        return;
                                    }
                                    array[i].ProductType = text;
                                }
                            }
                        }
                        try
                        {
                            BarcodePrinter barcodePrinter = new Printer_BiaoZhun_Tsc();
                            barcodePrinter.Print(array);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }
                    else
                    {
                        MessageBox.Show("你没有选中行，请选中要打印标签的数据行在进行打印", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
            }
        }

        private void menuDataSequenceByInput_Click(object sender, EventArgs e)
        {
            this.menuDataSequenceByInput.Checked = true;
            this.menuDataSequenceBySize.Checked = false;
            this.RefreshItems();
        }

        private void menuDataSequenceBySize_Click(object sender, EventArgs e)
        {
            this.menuDataSequenceByInput.Checked = false;
            this.menuDataSequenceBySize.Checked = true;
            this.RefreshItems();
        }

        private void menuShowSubtenseColumn_Click(object sender, EventArgs e)
        {
            if (this._dtOrderItems != null)
            {
                if (this.dgProducts.Columns["宽(短边)"].Visible)
                {
                    this.dgProducts.Columns["宽(短边)"].Visible = false;
                    this.dgProducts.Columns["高(短边)"].Visible = false;
                }
                else
                {
                    this.dgProducts.Columns["宽(短边)"].Visible = true;
                    this.dgProducts.Columns["高(短边)"].Visible = true;
                }
            }
        }

        private void menuShowData_Click(object sender, EventArgs e)
        {
            if (this.menuShowData.Checked)
            {
                this.menuShowData.Checked = false;
                this.menuShowData.Text = "暂时不显示所输入的数据";
            }
            else
            {
                this.menuShowData.Checked = true;
                this.menuShowData.Text = "显示所输入的数据";
            }
        }

        private void menuTypeSortArea_Click(object sender, EventArgs e)
        {
            Flowcards flowcards = new Flowcards();
            DataSet dataSet = null;
            try
            {
                dataSet = flowcards.GetTypeSubtotalOfFlowcard(this._nFlowcardID);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            flowcards.Dispose();
            if (dataSet != null)
            {
                if (dataSet.Tables.Count == 2)
                {
                    string text = "※ 分类信息如下：\r\n";
                    int num = 0;
                    decimal d = 0m;
                    decimal d2 = 0m;
                    decimal d3 = 0m;
                    string text2;
                    for (int i = 0; i < dataSet.Tables[0].Rows.Count; i++)
                    {
                        int num2 = Convert.ToInt32(dataSet.Tables[0].Rows[i][1]);
                        num += num2;
                        decimal num3 = Convert.ToDecimal(dataSet.Tables[0].Rows[i][2]);
                        d += num3;
                        decimal d4 = Convert.ToDecimal(dataSet.Tables[0].Rows[i][3]);
                        d2 += d4;
                        decimal d5 = Convert.ToDecimal(dataSet.Tables[0].Rows[i][4]);
                        decimal d6 = Convert.ToDecimal((num3 * d5).ToString("F4"));
                        d3 += d6;
                        text2 = text;
                        text = string.Concat(new string[]
                        {
                            text2,
                            "\r\n",
                            (i + 1).ToString().PadRight(3, ' '),
                            "、类型：",
                            CommonMethod.PadSpaceOnRight(dataSet.Tables[0].Rows[i][0].ToString(), 16),
                            "数量：",
                            CommonMethod.PadSpaceOnRight(dataSet.Tables[0].Rows[i][1].ToString(), 10),
                            "面积：",
                            CommonMethod.PadSpaceOnRight(dataSet.Tables[0].Rows[i][2].ToString(), 16),
                            "周长：",
                            CommonMethod.PadSpaceOnRight(dataSet.Tables[0].Rows[i][3].ToString(), 16),
                            "价格：",
                            dataSet.Tables[0].Rows[i][4].ToString()
                        });
                    }
                    text2 = text;
                    text = string.Concat(new string[]
                    {
                        text2,
                        "\r\n\r\n     ★数量为：",
                        num.ToString(),
                        "；   总平方为：",
                        d.ToString(),
                        "；   总周长为：",
                        d2.ToString(),
                        "；   金额为：",
                        d3.ToString()
                    });
                    text += "\r\n\r\n※ 工艺分类信息如下：\r\n";
                    decimal d7 = 0m;
                    decimal num4 = 0m;
                    for (int i = 0; i < dataSet.Tables[1].Rows.Count; i++)
                    {
                        d7 += Convert.ToDecimal(dataSet.Tables[1].Rows[i][1]);
                        num4 = Convert.ToDecimal((num4 + Convert.ToDecimal(dataSet.Tables[1].Rows[i][1]) * Convert.ToDecimal(dataSet.Tables[1].Rows[i][2])).ToString("F4"));
                        text2 = text;
                        text = string.Concat(new string[]
                        {
                            text2,
                            "\r\n",
                            (i + 1).ToString().PadRight(3, ' '),
                            "、工艺：",
                            CommonMethod.PadSpaceOnRight(dataSet.Tables[1].Rows[i][0].ToString(), 16),
                            "数量：",
                            CommonMethod.PadSpaceOnRight(Convert.ToDecimal(dataSet.Tables[1].Rows[i][1]).ToString(), 16),
                            "价格：",
                            dataSet.Tables[1].Rows[i][2].ToString()
                        });
                    }
                    text2 = text;
                    text = string.Concat(new string[]
                    {
                        text2,
                        "\r\n\r\n     ★加工工艺的数量为：",
                        d7.ToString(),
                        "；   加工工艺的金额为：",
                        num4.ToString()
                    });
                    MessageBox.Show(text, "Success 总金额为：" + (d3 + num4).ToString(), MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                else
                {
                    MessageBox.Show("可能没有数据信息！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            if (dataSet != null)
            {
                dataSet.Dispose();
            }
        }

        private void menuNumberOfRows_Click(object sender, EventArgs e)
        {
            Flowcards flowcards = new Flowcards();
            double num = 0.0;
            try
            {
                num = (double)flowcards.GetNumberOfRows(this._nFlowcardID);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            flowcards.Dispose();
            MessageBox.Show(num.ToString(), "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }

        private void lkUpdateRemark_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            string text = InputBox.ShowBox("请输入你要更改的备注...", this._sFlowcardRemark);
            if (text != null)
            {
                Flowcards flowcards = new Flowcards();
                bool flag = false;
                try
                {
                    flag = flowcards.UpdateRemark(this._sFlowcardCode, text.Trim());
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                flowcards.Dispose();
                if (flag)
                {
                    MessageBox.Show("更改备注成功！", "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    this._sFlowcardRemark = text.Trim();
                    this.txtRemark.Text = ((this._sOrderRemark != "" && this._sFlowcardRemark != "") ? (this._sOrderRemark + "/" + this._sFlowcardRemark) : (this._sOrderRemark + this._sFlowcardRemark));
                }
            }
        }

        private void btnInvoiceList_Click(object sender, EventArgs e)
        {
            try
            {
                string text = this.dgProducts.Rows[0].Cells[0].Value.ToString();
            }
            catch
            {
                MessageBox.Show("表格中没有数据", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            int rowIndex = this.dgProducts.CurrentCell.RowIndex;
            FormInvoiceList formInvoiceList = new FormInvoiceList(this.dgProducts.Rows[rowIndex].Cells[0].Value.ToString());
            formInvoiceList.MdiParent = base.MdiParent;
            formInvoiceList.Show();
            formInvoiceList.Activate();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void dgProducts_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            this._sOriginalValue = this.dgProducts.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString().Trim();
        }

        private void dgProducts_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
        }

        private void dgProducts_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
        }

        private void dgProducts_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
        }
    }
}
